package com.zsy.petshop_01.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsy.petshop_01.common.result.ResponseResult;
import com.zsy.petshop_01.manager.OrderMessageManager;
import com.zsy.petshop_01.model.entity.OrderMessage;
import com.zsy.petshop_01.model.normal.PageResult;
import com.zsy.petshop_01.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMessageManager orderMessageManager;

    @Override
    public ResponseResult<?> selectPageOrder(Integer currentPage, Integer pageSize, Integer userId, String time, Boolean isDispose, Boolean isAccomplish) {
        IPage<OrderMessage> page = new Page<>(currentPage,pageSize);
        IPage<OrderMessage> orderMessageIPage = orderMessageManager.selectPage(page, userId, time, isDispose, isAccomplish);
        PageResult<OrderMessage> pageResult = new PageResult<>();
        pageResult.setTotal(orderMessageIPage.getTotal());
        pageResult.setCurrentPage(currentPage.longValue());
        pageResult.setPageSize(pageSize.longValue());
        pageResult.setList(orderMessageIPage.getRecords());
        return ResponseResult.success(pageResult);
    }

    @Override
    public ResponseResult<?> selectPageUserOrder(Integer currentPage, Integer pageSize, String time, Boolean isDispose, Boolean isAccomplish, HttpServletRequest request) {
        Integer userId = Integer.parseInt(request.getHeader("user_id"));
        IPage<OrderMessage> page = new Page<>(currentPage,pageSize);
        IPage<OrderMessage> orderMessageIPage = orderMessageManager.selectPage(page, userId, time, isDispose, isAccomplish);
        PageResult<OrderMessage> pageResult = new PageResult<>();
        pageResult.setTotal(orderMessageIPage.getTotal());
        pageResult.setCurrentPage(currentPage.longValue());
        pageResult.setPageSize(pageSize.longValue());
        pageResult.setList(orderMessageIPage.getRecords());
        return ResponseResult.success(pageResult);
    }

    @Override
    public ResponseResult<?> disposeOrder(Integer id) {
        orderMessageManager.disposeOrder(id);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<?> accomplishOrder(Integer id) {
        orderMessageManager.accomplishOrder(id);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<?> cancelOrder(Integer id,String reason) {
        return orderMessageManager.cancelOrder(id, reason);
    }
}
